###This produces the tabular results for candidate stereotyping of issues in Replication Data for: Disputed Ownership: Parties, Issues, and Traits in the Minds of Voters by Stephen N. Goggin and Alexander G. Theodoridis

library(foreign)
library(car)
library(boot)

setwd("~/Desktop/DisputedOwnership_Replication")

cces <- read.csv("issue_stereotyping.csv")

#Note these levels of candidate and respondent party
#candparty -> 1 = Dem, 2 = Rep, 3 = None
#pid3lean -> -1 = Dem, 0 = Ind, 1 = Rep
candparty <- cces$AGT_party_iocs_text

attach(cces)

##Creating Trait Variables

I_1 <- AGT_iocs_issues_1 - 4
I_2 <- AGT_iocs_issues_3 - 4
I_3 <- AGT_iocs_issues_4 - 4
I_4 <- AGT_iocs_issues_5 - 4
I_5 <- AGT_iocs_issues_9 - 4
I_6 <- AGT_iocs_issues_11 - 4
I_7 <- AGT_iocs_issues_13 - 4
I_8 <- AGT_iocs_issues_16 - 4
I_9 <- AGT_iocs_issues_17 - 4
I_10 <- AGT_iocs_issues_18 - 4

####################
#Create Data Frame with Essential Variables
cces_ready <- data.frame(V101,candparty,pid3leanPost,I_1,I_2,I_3,I_4,I_5,I_6,I_7,I_8,I_9,I_10)

#Creating Vectors for Output
MDiff_1 <- NULL
MDiff_2 <- NULL
MDiff_3 <- NULL
MDiff_4 <- NULL
MDiff_5 <- NULL
MDiff_6 <- NULL
MDiff_7 <- NULL
MDiff_8 <- NULL
MDiff_9 <- NULL
MDiff_10 <- NULL

####################
#Bootstrapping it all

for (i in 1:10000){
	
tempdata <- sample(1:nrow(cces_ready),nrow(cces_ready),replace=T)

partisans <- cces_ready[tempdata,]

D_D <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==2)
D_R <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==1)
R_R <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==1)
R_D <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==2)

D_D_1 <- mean(D_D$I_1, na.rm=T)
D_R_1 <- mean(D_R$I_1, na.rm=T)
R_R_1 <- mean(R_R$I_1, na.rm=T)
R_D_1 <- mean(R_D$I_1, na.rm=T)

D_D_2 <- mean(D_D$I_2, na.rm=T)
D_R_2 <- mean(D_R$I_2, na.rm=T)
R_R_2 <- mean(R_R$I_2, na.rm=T)
R_D_2 <- mean(R_D$I_2, na.rm=T)

D_D_3 <- mean(D_D$I_3, na.rm=T)
D_R_3 <- mean(D_R$I_3, na.rm=T)
R_R_3 <- mean(R_R$I_3, na.rm=T)
R_D_3 <- mean(R_D$I_3, na.rm=T)

D_D_4 <- mean(D_D$I_4, na.rm=T)
D_R_4 <- mean(D_R$I_4, na.rm=T)
R_R_4 <- mean(R_R$I_4, na.rm=T)
R_D_4 <- mean(R_D$I_4, na.rm=T)

D_D_5 <- mean(D_D$I_5, na.rm=T)
D_R_5 <- mean(D_R$I_5, na.rm=T)
R_R_5 <- mean(R_R$I_5, na.rm=T)
R_D_5 <- mean(R_D$I_5, na.rm=T)

D_D_6 <- mean(D_D$I_6, na.rm=T)
D_R_6 <- mean(D_R$I_6, na.rm=T)
R_R_6 <- mean(R_R$I_6, na.rm=T)
R_D_6 <- mean(R_D$I_6, na.rm=T)

D_D_7 <- mean(D_D$I_7, na.rm=T)
D_R_7 <- mean(D_R$I_7, na.rm=T)
R_R_7 <- mean(R_R$I_7, na.rm=T)
R_D_7 <- mean(R_D$I_7, na.rm=T)

D_D_8 <- mean(D_D$I_8, na.rm=T)
D_R_8 <- mean(D_R$I_8, na.rm=T)
R_R_8 <- mean(R_R$I_8, na.rm=T)
R_D_8 <- mean(R_D$I_8, na.rm=T)

D_D_9 <- mean(D_D$I_9, na.rm=T)
D_R_9 <- mean(D_R$I_9, na.rm=T)
R_R_9 <- mean(R_R$I_9, na.rm=T)
R_D_9 <- mean(R_D$I_9, na.rm=T)

D_D_10 <- mean(D_D$I_10, na.rm=T)
D_R_10 <- mean(D_R$I_10, na.rm=T)
R_R_10 <- mean(R_R$I_10, na.rm=T)
R_D_10 <- mean(R_D$I_10, na.rm=T)

D_1 <- D_D_1 - D_R_1
R_1 <- R_R_1 - R_D_1

D_2 <- D_D_2 - D_R_2
R_2 <- R_R_2 - R_D_2

D_3 <- D_D_3 - D_R_3
R_3 <- R_R_3 - R_D_3

D_4 <- D_D_4 - D_R_4
R_4 <- R_R_4 - R_D_4

D_5 <- D_D_5 - D_R_5
R_5 <- R_R_5 - R_D_5

D_6 <- D_D_6 - D_R_6
R_6 <- R_R_6 - R_D_6

D_7 <- D_D_7 - D_R_7
R_7 <- R_R_7 - R_D_7

D_8 <- D_D_8 - D_R_8
R_8 <- R_R_8 - R_D_8

D_9 <- D_D_9 - D_R_9
R_9 <- R_R_9 - R_D_9

D_10 <- D_D_10 - D_R_10
R_10 <- R_R_10 - R_D_10

Diff_1 <- D_1 - R_1
Diff_2 <- D_2 - R_2
Diff_3 <- D_3 - R_3
Diff_4 <- D_4 - R_4
Diff_5 <- D_5 - R_5
Diff_6 <- D_6 - R_6
Diff_7 <- D_7 - R_7
Diff_8 <- D_8 - R_8
Diff_9 <- D_9 - R_9
Diff_10 <- D_10 - R_10

Diffs <- c(Diff_1,Diff_2,Diff_3,Diff_4,Diff_5,Diff_6,Diff_7,Diff_8,Diff_9,Diff_10)

Mean_Diff <- mean(Diffs)

MDiff_1 <- c(MDiff_1, Diff_1 - Mean_Diff)
MDiff_2 <- c(MDiff_2, Diff_2 - Mean_Diff)
MDiff_3 <- c(MDiff_3, Diff_3 - Mean_Diff)
MDiff_4 <- c(MDiff_4, Diff_4 - Mean_Diff)
MDiff_5 <- c(MDiff_5, Diff_5 - Mean_Diff)
MDiff_6 <- c(MDiff_6, Diff_6 - Mean_Diff)
MDiff_7 <- c(MDiff_7, Diff_7 - Mean_Diff)
MDiff_8 <- c(MDiff_8, Diff_8 - Mean_Diff)
MDiff_9 <- c(MDiff_9, Diff_9 - Mean_Diff)
MDiff_10 <- c(MDiff_10, Diff_10 - Mean_Diff)
		
}

####################
#Calculating the Actual Differences in Sample

D_D <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==2)
D_R <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==1)
R_R <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==1)
R_D <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==2)

D_D_1 <- mean(D_D$I_1, na.rm=T)
D_R_1 <- mean(D_R$I_1, na.rm=T)
R_R_1 <- mean(R_R$I_1, na.rm=T)
R_D_1 <- mean(R_D$I_1, na.rm=T)

D_D_2 <- mean(D_D$I_2, na.rm=T)
D_R_2 <- mean(D_R$I_2, na.rm=T)
R_R_2 <- mean(R_R$I_2, na.rm=T)
R_D_2 <- mean(R_D$I_2, na.rm=T)

D_D_3 <- mean(D_D$I_3, na.rm=T)
D_R_3 <- mean(D_R$I_3, na.rm=T)
R_R_3 <- mean(R_R$I_3, na.rm=T)
R_D_3 <- mean(R_D$I_3, na.rm=T)

D_D_4 <- mean(D_D$I_4, na.rm=T)
D_R_4 <- mean(D_R$I_4, na.rm=T)
R_R_4 <- mean(R_R$I_4, na.rm=T)
R_D_4 <- mean(R_D$I_4, na.rm=T)

D_D_5 <- mean(D_D$I_5, na.rm=T)
D_R_5 <- mean(D_R$I_5, na.rm=T)
R_R_5 <- mean(R_R$I_5, na.rm=T)
R_D_5 <- mean(R_D$I_5, na.rm=T)

D_D_6 <- mean(D_D$I_6, na.rm=T)
D_R_6 <- mean(D_R$I_6, na.rm=T)
R_R_6 <- mean(R_R$I_6, na.rm=T)
R_D_6 <- mean(R_D$I_6, na.rm=T)

D_D_7 <- mean(D_D$I_7, na.rm=T)
D_R_7 <- mean(D_R$I_7, na.rm=T)
R_R_7 <- mean(R_R$I_7, na.rm=T)
R_D_7 <- mean(R_D$I_7, na.rm=T)

D_D_8 <- mean(D_D$I_8, na.rm=T)
D_R_8 <- mean(D_R$I_8, na.rm=T)
R_R_8 <- mean(R_R$I_8, na.rm=T)
R_D_8 <- mean(R_D$I_8, na.rm=T)

D_D_9 <- mean(D_D$I_9, na.rm=T)
D_R_9 <- mean(D_R$I_9, na.rm=T)
R_R_9 <- mean(R_R$I_9, na.rm=T)
R_D_9 <- mean(R_D$I_9, na.rm=T)

D_D_10 <- mean(D_D$I_10, na.rm=T)
D_R_10 <- mean(D_R$I_10, na.rm=T)
R_R_10 <- mean(R_R$I_10, na.rm=T)
R_D_10 <- mean(R_D$I_10, na.rm=T)

D_1 <- D_D_1 - D_R_1
R_1 <- R_R_1 - R_D_1

D_2 <- D_D_2 - D_R_2
R_2 <- R_R_2 - R_D_2

D_3 <- D_D_3 - D_R_3
R_3 <- R_R_3 - R_D_3

D_4 <- D_D_4 - D_R_4
R_4 <- R_R_4 - R_D_4

D_5 <- D_D_5 - D_R_5
R_5 <- R_R_5 - R_D_5

D_6 <- D_D_6 - D_R_6
R_6 <- R_R_6 - R_D_6

D_7 <- D_D_7 - D_R_7
R_7 <- R_R_7 - R_D_7

D_8 <- D_D_8 - D_R_8
R_8 <- R_R_8 - R_D_8

D_9 <- D_D_9 - D_R_9
R_9 <- R_R_9 - R_D_9

D_10 <- D_D_10 - D_R_10
R_10 <- R_R_10 - R_D_10

Diff_1 <- D_1 - R_1
Diff_2 <- D_2 - R_2
Diff_3 <- D_3 - R_3
Diff_4 <- D_4 - R_4
Diff_5 <- D_5 - R_5
Diff_6 <- D_6 - R_6
Diff_7 <- D_7 - R_7
Diff_8 <- D_8 - R_8
Diff_9 <- D_9 - R_9
Diff_10 <- D_10 - R_10

Diffs <- c(Diff_1,Diff_2,Diff_3,Diff_4,Diff_5,Diff_6,Diff_7,Diff_8,Diff_9,Diff_10)

Mean_Diff <- mean(Diffs)

#The actual differences of interest
ADiff_1 <- Diff_1 - Mean_Diff
ADiff_2 <- Diff_2 - Mean_Diff
ADiff_3 <- Diff_3 - Mean_Diff
ADiff_4 <- Diff_4 - Mean_Diff
ADiff_5 <- Diff_5 - Mean_Diff
ADiff_6 <- Diff_6 - Mean_Diff
ADiff_7 <- Diff_7 - Mean_Diff
ADiff_8 <- Diff_8 - Mean_Diff
ADiff_9 <- Diff_9 - Mean_Diff
ADiff_10 <- Diff_10 - Mean_Diff

####################
#Comparing the Actual Differences and Bootstrapped Bounds

issue <- c("National Defense","Moral Values","Immigration","Taxes","Budget Deficit","The Economy","Education","Environment","Healthcare","Poverty")

CI95_lower <- c(quantile(MDiff_1,.025),quantile(MDiff_2,.025)
,quantile(MDiff_3,.025),quantile(MDiff_4,.025),quantile(MDiff_5,.025),quantile(MDiff_6,.025),quantile(MDiff_7,.025),quantile(MDiff_8,.025),quantile(MDiff_9,.025),quantile(MDiff_10,.025))

CI95_upper <- c(quantile(MDiff_1,.975),quantile(MDiff_2,.975)
,quantile(MDiff_3,.975),quantile(MDiff_4,.975),quantile(MDiff_5,.975),quantile(MDiff_6,.975),quantile(MDiff_7,.975),quantile(MDiff_8,.975),quantile(MDiff_9,.975),quantile(MDiff_10,.975))

ownership_estimate <- c(ADiff_1,ADiff_2,ADiff_3,ADiff_4,ADiff_5,ADiff_6,ADiff_7,ADiff_8,ADiff_9,ADiff_10)

D_ratingdiff <- c(D_1, D_2, D_3, D_4, D_5, D_6, D_7, D_8, D_9, D_10)

R_ratingdiff <- c(R_1, R_2, R_3, R_4, R_5, R_6, R_7, R_8, R_9, R_10)

raw_Diff <- c(Diff_1,Diff_2,Diff_3,Diff_4,Diff_5,Diff_6,Diff_7,Diff_8,Diff_9,Diff_10)

ownership <- data.frame(issue,ownership_estimate, CI95_lower,CI95_upper,raw_Diff,D_ratingdiff,R_ratingdiff)

write.csv(ownership,"IssueOwnership_CandidateStereotyping.csv")


